/**
* 面板弹出管理类
*/
class PopUpUtils {

    static masker: egret.Shape = new egret.Shape();

    /**
     * 添加面板方法
     * panel
     * dark 背景是否变黑
     * effectType 0：没有动画 1:从中间轻微弹出 2：从中间猛烈弹出  3：从左向右 4：从右向左 5、从上到下 6、从下到上
     */
    static addPopUp(panel, dark: boolean = false, effectType: number = 0, isAlert: boolean = false): void {
        panel.scaleX = 1;
        panel.scaleY = 1;
        panel.x = 0;
        panel.y = 0;
        panel.alpha = 1;
        egret.Tween.removeTweens(panel);

        if (dark) {
            PopUpUtils.masker.touchEnabled = true;
            PopUpUtils.masker.visible = true;

            this.redrawMasker();

            if (!LayerManager.i.maskLayer.contains(PopUpUtils.masker)) {
                LayerManager.i.maskLayer.addChild(PopUpUtils.masker);
            }
        }

        egret.Tween.removeTweens(PopUpUtils.masker);

        LayerManager.i.panelLayer.addChild(panel);

        let popUpWidth = panel.width;
        let popUpHeight = panel.height;

        //以下是弹窗动画
        let leftX: number = acelab.width / 2 - popUpWidth / 2;
        let upY: number = acelab.height / 2 - popUpHeight / 2;

        switch (effectType) {
            case 0:
                break;
            case 1:
                panel.alpha = 1;
                panel.scaleX = 0.5;
                panel.scaleY = 0.5;
                panel.x = panel.x + popUpWidth / 4;
                panel.y = panel.y + popUpHeight / 4;
                egret.Tween.get(panel).to({
                    alpha: 1,
                    scaleX: 1,
                    scaleY: 1,
                    x: panel.x - popUpWidth / 4,
                    y: panel.y - popUpHeight / 4
                }, 300, egret.Ease.backOut);
                break;
            case 2:
                panel.alpha = 0;
                panel.scaleX = 0.5;
                panel.scaleY = 0.5;
                panel.x = panel.x + popUpWidth / 4;
                panel.y = panel.y + popUpHeight / 4;
                egret.Tween.get(panel).to({
                    alpha: 1,
                    scaleX: 1,
                    scaleY: 1,
                    x: panel.x - popUpWidth / 4,
                    y: panel.y - popUpHeight / 4
                }, 600, egret.Ease.elasticOut);
                break;
            case 3:
                if (isAlert) {
                    panel.x = -popUpWidth;
                    egret.Tween.get(panel).to({x: leftX}, 500, egret.Ease.cubicOut);
                }
                else {
                    panel.x = -popUpWidth;
                    egret.Tween.get(panel).to({x: 0}, 500, egret.Ease.cubicOut);
                }
                break;
            case 4:
                if (isAlert) {
                    panel.x = popUpWidth;
                    egret.Tween.get(panel).to({x: leftX}, 500, egret.Ease.cubicOut);
                }
                else {
                    panel.x = popUpWidth;
                    egret.Tween.get(panel).to({x: 0}, 500, egret.Ease.cubicOut);
                }
                break;
            case 5:
                if (isAlert) {
                    panel.y = -popUpHeight;
                    egret.Tween.get(panel).to({y: upY}, 500, egret.Ease.cubicOut);
                }
                else {
                    panel.y = -popUpHeight;
                    egret.Tween.get(panel).to({y: 0}, 500, egret.Ease.cubicOut);
                }
                break;
            case 6:
                if (isAlert) {
                    panel.y = acelab.height;
                    egret.Tween.get(panel).to({y: upY}, 500, egret.Ease.cubicOut);
                }
                else {
                    panel.y = popUpHeight;
                    egret.Tween.get(panel).to({y: 0}, 500, egret.Ease.cubicOut);
                }
                break;
            default:
                break;
        }

    }

    /**
     * 移除面板方法
     * @param panel
     * @param effectType 0：没有动画 1:从中间缩小消失 2：  3：从左向右 4：从右向左 5、从上到下 6、从下到上
     */
    static removePopUp(panel, effectType: number = 0): void {
        let my = this;

        function close() {
            //判断是否包含panel
            if (LayerManager.i.panelLayer.contains(panel)) {
                LayerManager.i.panelLayer.removeChild(panel);
            }

            let onComplete: Function = function () {
                if (LayerManager.i.maskLayer.contains(PopUpUtils.masker)) {
                    LayerManager.i.maskLayer.removeChild(PopUpUtils.masker);
                    PopUpUtils.masker.alpha = 1;
                }
            };

            if (LayerManager.i.panelLayer.numChildren <= 0 && PopUpUtils.masker) {
                egret.Tween.get(PopUpUtils.masker).to({alpha: 0}, 100).call(onComplete, my);
            }
        }

        //以下是弹窗动画
        switch (effectType) {
            case 0:
                break;
            case 1:
                egret.Tween.get(panel).to({
                    alpha: 0,
                    scaleX: 0,
                    scaleY: 0,
                    x: panel.x + panel.width / 2,
                    y: panel.y + panel.height / 2
                }, 200).call(close, this);
                break;
            case 2:
                break;
            case 3:
                egret.Tween.get(panel).to({x: panel.width}, 500, egret.Ease.cubicOut).call(close, my);
                break;
            case 4:
                egret.Tween.get(panel).to({x: -panel.width}, 500, egret.Ease.cubicOut).call(close, my);
                break;
            case 5:
                egret.Tween.get(panel).to({y: panel.height}, 500, egret.Ease.cubicOut).call(close, my);
                break;
            case 6:
                egret.Tween.get(panel).to({y: -panel.height}, 500, egret.Ease.cubicOut).call(close, my);
                break;
            default:
                break;
        }
    }

    static redrawMasker() {
        PopUpUtils.masker.graphics.clear();
        PopUpUtils.masker.graphics.beginFill(0x000000, 0.6);
        PopUpUtils.masker.graphics.drawRect(0, 0, acelab.width, acelab.height);
        PopUpUtils.masker.graphics.endFill();
    }
}